package com.qcxk.yuqian.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcxk.yuqian.system.component.behaviour.BehaviourOperator;
import com.qcxk.yuqian.system.component.constant.SystemConstant;
import com.qcxk.yuqian.system.entity.Role;
import com.qcxk.yuqian.system.service.RoleService;
import com.qcxk.yuqian.system.utils.ThmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RoleService roleService;

    @BehaviourOperator(desc = "查询角色信息")
    @RequestMapping(value = "/loadRole")
    public JSONObject loadRole(Role role) {
        JSONObject json = new JSONObject();
        role = roleService.getById(role.getId());
        if (role != null) {
            json.put("code", "0");
            json.put("data", role);
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "查询角色列表")
    @RequestMapping(value = "/queryRoleList")
    public JSONObject queryRoleList(Page page, Role role) {
        JSONObject json = new JSONObject();
        page = roleService.queryRoleList(page, role);
        json.put("code", "0");
        json.put("current", page.getCurrent());
        json.put("size", page.getSize());
        json.put("total", page.getTotal());
        json.put("dataList", page.getRecords());
        return json;
    }

    @BehaviourOperator(desc = "修改角色")
    @RequestMapping(value = "/updateRole")
    public JSONObject updateRole(Role role) {
        JSONObject json = new JSONObject();
        if (roleService.updateRole(role)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "增加角色")
    @RequestMapping(value = "/addRole")
    public JSONObject addRole(Role role) {
        JSONObject json = new JSONObject();
        role.setCtime(ThmsUtil.getDate());
        if (roleService.save(role)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "删除角色")
    @RequestMapping(value = "/delRole")
    public JSONObject delRole(Role role) {
        JSONObject json = new JSONObject();
        if (roleService.removeRole(role.getId())) {

            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "角色授权")
    @RequestMapping(value = "/grantAuthToRole")
    public JSONObject grantAuthToRole(Integer roleId, Integer[] idarr) {
        JSONObject json = new JSONObject();

        if (roleService.grantAuthToRole(roleId, idarr)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "分配角色")
    @RequestMapping(value = "/grantRoleForUser")
    public JSONObject grantRoleForUser(Integer userId, Integer roleId) {
        JSONObject json = new JSONObject();

        if (roleService.grantRoleForUser(userId, roleId)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }
}
